package com.koron.sinoprobe.web.ledger.service.impl;


import cn.hutool.core.util.StrUtil;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProjectEquipment;
import com.koron.sinoprobe.web.ledger.bean.vo.SpProjectVO;
import com.koron.sinoprobe.web.ledger.mapper.SpProjectEquipmentMapper;
import com.koron.sinoprobe.web.ledger.service.SpProjectEquipmentService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目设备从表(SpProjectEquipment)表服务实现类
 *
 * @author jzp
 * @since 2022-06-07 11:55:59
 */
@Service
public class SpProjectEquipmentServiceImpl implements SpProjectEquipmentService {

    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public SpProjectEquipment getById(SessionFactory factory, String id) {
        SpProjectEquipmentMapper mapper = factory.getMapper(SpProjectEquipmentMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<SpProjectEquipment> list(SessionFactory factory, SpProjectEquipment spProjectEquipment) {
        SpProjectEquipmentMapper mapper = factory.getMapper(SpProjectEquipmentMapper.class);
        return mapper.selectList(spProjectEquipment);
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, SpProjectEquipment spProjectEquipment) {
        SpProjectEquipmentMapper mapper = factory.getMapper(SpProjectEquipmentMapper.class);
        return mapper.insertOrUpdate(spProjectEquipment) > 0;
    }

    @Override
    @TaskAnnotation("saveOrUpdateBatch")
    public boolean saveOrUpdateBatch(SessionFactory factory, SpProjectVO spProjectVO) {
        Assert.notNull(spProjectVO.getId(), "项目id不能为空");
        removeByProjectId(factory, spProjectVO.getId());
        List<SpProjectEquipment> projectEquipment = spProjectVO.getProjectEquipment();
        if (CollectionUtils.isEmpty(projectEquipment)) {
            return false;
        }
        projectEquipment.forEach(e -> e.setProjectId(spProjectVO.getId()));
        SpProjectEquipmentMapper mapper = factory.getMapper(SpProjectEquipmentMapper.class);
        return mapper.insertOrUpdateBatch(projectEquipment) > 0;
    }

    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpProjectEquipment spProjectEquipment) {
        SpProjectEquipmentMapper mapper = factory.getMapper(SpProjectEquipmentMapper.class);
        return mapper.update(spProjectEquipment) > 0;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        SpProjectEquipmentMapper mapper = factory.getMapper(SpProjectEquipmentMapper.class);
        return mapper.hidden(id) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        SpProjectEquipmentMapper mapper = factory.getMapper(SpProjectEquipmentMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByProjectId")
    public boolean removeByProjectId(SessionFactory factory, String projectId) {
        if (StrUtil.isEmpty(projectId)) {
            return false;
        }
        SpProjectEquipment spProjectEquipment = new SpProjectEquipment();
        spProjectEquipment.setProjectId(projectId);
        List<SpProjectEquipment> list = list(factory, spProjectEquipment);

        return deleteBatchIds(factory, list.stream().map(SpProjectEquipment::getId).collect(Collectors.toList()));
    }

}
